package com.proposition.exam.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.proposition.commons.exam.result.Result;
import com.proposition.commons.exam.result.ResultUtil;
import com.proposition.commons.exam.util.ExamAddUtil;
import com.proposition.commons.exam.util.ExamFieldVolidateUtil;
import com.proposition.commons.exam.util.ExamUtil;
import com.proposition.commons.model.ExamAnswer10;
import com.proposition.commons.model.ExamAnswer10Original;
import com.proposition.exam.mapper.ExamAnswer10Mapper;
import com.proposition.exam.service.ExamAnswer10Service;
import com.proposition.support.security.CurrentUser;
import com.proposition.support.utils.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: 陈玉林
 * @modifiedBy:
 * @date: Create In 10:31 2017/11/3
 * @description:
 */
@Service
public class ExamAnswer10ServiceImpl extends ServiceImpl<ExamAnswer10Mapper, ExamAnswer10> implements ExamAnswer10Service {
    @Transactional
    @Override
    public Result add(String[] answerWD2, Float[] answerWDScore2, String[] answerDXSF, Float[] answerDXSFScore, ExamAnswer10 exam, ExamAnswer10Original examOriginal) {

        String stem = ExamFieldVolidateUtil.getIllegalStem(exam.getStem());
        exam.setStem(stem);
        ExamAddUtil.volidateUniqueStem(exam);
        ExamUtil.volidateOutlineCode(exam.getOutlineCode());

        //验证题干
        String categoryStr = ExamUtil.getCategoryStr(exam.getCategoryId());
        String id = UUIDUtil.getExamUUIDByCategory(categoryStr);
        String userIdImport = CurrentUser.INSTANCE.getUserId();

        Integer categoryId = exam.getCategoryId();
        if (categoryId != 10) {
            exam.setSinglePloyFiveCategoryId(0);
        }
        if (categoryId != 9) {
            exam.setExamCategorySinglePloyPediatricSubclassId(0);
        }
        //设置答案
        if (categoryId == 2 || categoryId == 3 || categoryId == 4 || categoryId == 5 || categoryId == 6) {
            Object[] obj = ExamUtil.setAnwerTenAnswer(exam, examOriginal, answerWD2, answerWDScore2);
            exam=(ExamAnswer10)obj[0];
            examOriginal=(ExamAnswer10Original) obj[1];
        } else if (categoryId == 9 || categoryId == 10 || categoryId == 11) {
            Object[] obj = ExamUtil.setAnwerTenAnswer(exam, examOriginal, answerDXSF, answerDXSFScore);
            exam=(ExamAnswer10)obj[0];
            examOriginal=(ExamAnswer10Original) obj[1];
        }
        exam.setId(id);//设置id
        exam.setUserIdImport(userIdImport);//设置当前导入用户id
        exam.insert();

        examOriginal.setId(id);
        examOriginal.setUserIdImport(userIdImport);
        examOriginal.insert();
        return ResultUtil.sucess();
    }
}
